import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class L491 {

    public static void main(String[] args) {
//        int[] nums = {1, 2, 3};
        int[] nums = {0,1};
        List<List<Integer>> subsequences = new Solution().permute(nums);
        for (List<Integer> subsequence : subsequences) {
            System.out.println(subsequence.toString());
        }


    }

    static class Solution {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> tmp = new LinkedList<>();

        public List<List<Integer>> permute(int[] nums) {
            boolean[] used = new boolean[nums.length];
            backTracking(nums, 0, used);
            return result;
        }

        private void backTracking(int[] nums, int index, boolean[] used) {
            if (tmp.size() >= nums.length) {
                result.add(new LinkedList<>(tmp));
                return;
            }
            for (int i = index; i < nums.length; i++) {
                if (used[i]) {
                    continue;
                }
                tmp.add(nums[i]);
                used[i] = true;
                backTracking(nums, 0, used);
                tmp.removeLast();
                used[i] = false;
            }

        }
    }
}
